package org.qpcr.community.device.web.request;

import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.qpcr.community.device.enums.CalibrationCommandStatuEnums;
import org.qpcr.community.device.enums.CalibrationCommandTypeEnums;
import org.qpcr.community.device.web.calibration.MessageHeader;
import org.qpcr.community.device.web.calibration.RequestBody;
import org.qpcr.community.device.web.calibration.TemperatureCalibrationSequence;

import java.util.HashMap;
import java.util.Map;

@Data
@Schema(description = "设备校准请求")
public class CalibrationRequest {

    private MessageHeader header;
    private RequestBody body;

    public String toJsonString(){
        return JSONUtil.toJsonStr(this);
    }

    public String generateQueueName(){
        return String.format("device/%s/calibration/request", body.getDeviceId());
    }

    public static CalibrationRequest create(CalibrationCmdSendRequest request){
        CalibrationRequest calibrationRequest = new CalibrationRequest();
        calibrationRequest.setHeader(new MessageHeader());
        calibrationRequest.setBody(new RequestBody());
        calibrationRequest.getBody().setDeviceId(request.getDeviceId());
        calibrationRequest.getBody().setModuleCode(request.getModuleCode());
        calibrationRequest.getBody().setCommandType(request.getCommandType());
        CalibrationCommandTypeEnums commandType = CalibrationCommandTypeEnums.getByType(request.getCommandType());
        switch (commandType){
            case CALIBRATION:
            case TEST:
                RequestBody body = calibrationRequest.getBody();
                Map<String,Object> data = new HashMap<>();
                TemperatureCalibrationSequence sequence = null == request.getSequence() ? TemperatureCalibrationSequence.createDefault() : request.getSequence();
                data.put("sequence",sequence);
                data.put("algorithm",request.getAlgorithm());
                body.setData(data);
                break;
            case WRITE_PARAM :
            case BACKTRACK_PARAM :
            case FLUORESCENCE_WRITE_PARAM :
            case FLUORESCENCE_BACKTRACK_PARAM :
                calibrationRequest.getBody().setData(request.getWriteData());
                break;
            case FLUORESCENCE_CALIBRATION_PROCESS :
                calibrationRequest.getBody().setData(request.getExperimentalRecords());
                break;
            case SET_PRESS_HEAD_TORQUE :
                Map<String,Object> headData = new HashMap<>();
                headData.put("torqueNcm",request.getWriteData());
                calibrationRequest.getBody().setData(headData);
                break;
            case SET_AIR_PUMP_PRESSURE :
                Map<String,Object> airPumpPressure = new HashMap<>();
                airPumpPressure.put("pressureKpa",request.getWriteData());
                calibrationRequest.getBody().setData(airPumpPressure);
                break;
            case SET_UPLOAD_FREQUENCY :
                Map<String,Object> uploadFrequency = new HashMap<>();
                uploadFrequency.put("frequencyHz",request.getWriteData());
                calibrationRequest.getBody().setData(uploadFrequency);
                break;
            case PARAMETER_ANALYSIS :
                calibrationRequest.getBody().setData(null);
                break;

        }
        request.setMessageId(calibrationRequest.getHeader().getMessageId());
        request.setStatus(CalibrationCommandStatuEnums.SEND.getType());
        request.setRequestBody(calibrationRequest.toJsonString());
        return calibrationRequest;
    }
}